package com.hoshiicloud.user.controller.organization;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.organization.StoreOrganizationDepartmentes;
import com.hoshiicloud.user.param.organization.StoreOrganizationDepartmentesAddParam;
import com.hoshiicloud.user.param.organization.StoreOrganizationDepartmentesQueryParam;
import com.hoshiicloud.user.param.organization.StoreOrganizationDepartmentesUpdateParam;
import com.hoshiicloud.user.service.organization.StoreOrganizationDepartmentesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 店铺机构部门 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-05-15
 */
@Slf4j
@RestController
@RequestMapping("/organization/store-organization-departmentes")
@Api(value = "平台店铺机构部门", tags = {"平台店铺机构部门"})
@ApiSort(45)
public class StoreOrganizationDepartmentesController {
    @Autowired
    private StoreOrganizationDepartmentesService storeOrganizationDepartmentesService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询店铺机构部门
     */
    @ApiOperation(value = "分页查询店铺机构部门")
    @PostMapping("/getPage")
    public Response page(@RequestBody StoreOrganizationDepartmentesQueryParam param) {
        try {
            QueryWrapper<StoreOrganizationDepartmentes> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq((param.getOrganizationId() != null && param.getOrganizationId() != 0), StoreOrganizationDepartmentes::getOrganizationId, param.getOrganizationId())
                    .eq((param.getParentId() != null && param.getParentId() != 0), StoreOrganizationDepartmentes::getParentId, param.getParentId())
                    .eq(StringUtils.isNotBlank(param.getName()), StoreOrganizationDepartmentes::getName, param.getName())
                    .eq(StringUtils.isNotBlank(param.getOpenPermit()), StoreOrganizationDepartmentes::getOpenPermit, param.getOpenPermit())
                    .eq(StringUtils.isNotBlank(param.getCharge()), StoreOrganizationDepartmentes::getCharge, param.getCharge())
                    .eq(StringUtils.isNotBlank(param.getIdCard()), StoreOrganizationDepartmentes::getIdCard, param.getIdCard())
                    .eq(StringUtils.isNotBlank(param.getBankNumber()), StoreOrganizationDepartmentes::getBankNumber, param.getBankNumber())
                    .eq(StringUtils.isNotBlank(param.getBankName()), StoreOrganizationDepartmentes::getBankName, param.getBankName())
                    .eq(StringUtils.isNotBlank(param.getBankAddress()), StoreOrganizationDepartmentes::getBankAddress, param.getBankAddress())
                    .eq(StringUtils.isNotBlank(param.getTaxation()), StoreOrganizationDepartmentes::getTaxation, param.getTaxation())
                    .eq(StringUtils.isNotBlank(param.getPiaohao()), StoreOrganizationDepartmentes::getPiaohao, param.getPiaohao())
                    .eq(StringUtils.isNotBlank(param.getTelphone()), StoreOrganizationDepartmentes::getTelphone, param.getTelphone())
                    .eq(StringUtils.isNotBlank(param.getMobile()), StoreOrganizationDepartmentes::getMobile, param.getMobile())
                    .eq(StringUtils.isNotBlank(param.getFax()), StoreOrganizationDepartmentes::getFax, param.getFax())
                    .eq(StringUtils.isNotBlank(param.getHome()), StoreOrganizationDepartmentes::getHome, param.getHome())
                    .eq(StringUtils.isNotBlank(param.getEmail()), StoreOrganizationDepartmentes::getEmail, param.getEmail())
                    .eq(StringUtils.isNotBlank(param.getAddress()), StoreOrganizationDepartmentes::getAddress, param.getAddress());
            Page<StoreOrganizationDepartmentes> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(storeOrganizationDepartmentesService.getPage(page, queryWrapper, key));
        } catch (NumberFormatException e) {
            log.error("分页查询店铺机构(供应商)部门异常", e);
            return Response.failed("分页查询店铺机构(供应商)部门异常");
        }

    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询店铺机构部门
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询店铺机构部门")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(storeOrganizationDepartmentesService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增店铺机构部门")
    public Response saveStoreOrganizationDepartmentes(@RequestBody @Valid StoreOrganizationDepartmentesAddParam param) {
        StoreOrganizationDepartmentes entity;
        try {
            entity = BeanUtils.covertType(param, StoreOrganizationDepartmentes.class);
        } catch (Exception e) {
            log.error("新增店铺机构部门Bean转换异常", e);
            return Response.failed("新增店铺机构部门Bean转换异常");
        }
        return Response.data(storeOrganizationDepartmentesService.insert(entity));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改店铺机构部门
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改店铺机构部门")
    public Response updateStoreOrganizationDepartmentesById(@RequestBody @Valid StoreOrganizationDepartmentesUpdateParam param) {
        StoreOrganizationDepartmentes entity;
        try {
            entity = BeanUtils.covertType(param, StoreOrganizationDepartmentes.class);
        } catch (Exception e) {
            log.error("修改店铺机构部门Bean转换异常", e);
            return Response.failed("修改店铺机构部门Bean转换异常");
        }
        return Response.data(storeOrganizationDepartmentesService.updateById(entity));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除店铺机构部门
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除店铺机构部门")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        return Response.data(storeOrganizationDepartmentesService.deleteStoreOrganizationDepartmentesById(id));
    }

}
